package pt.utl.ist.project_ccu;

import java.util.ArrayList;

import pt.utl.ist.project_ccu.android_libs.Photo;
import pt.utl.ist.project_ccu.android_libs.Tag;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


public class PictureViewer extends Activity {

Photo picItem;
private TextView imageName;
private ImageView m_vwImage;


	public void onCreate(Bundle savedInstanceState){
	    super.onCreate(savedInstanceState);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
	    setContentView(R.layout.activity_picture_viewer);
	
	    Intent i = getIntent();
	
	    picItem = (Photo) i.getSerializableExtra("photo");
	    
	    imageName = (TextView)findViewById(R.id.imgname);
	    if(picItem.getName().equals("")) {
	    	imageName.setText("Name: " + picItem.getId());
	    }
	    else {
	    	imageName.setText("Name: " + picItem.getName());
	    }
	   
	    
	    m_vwImage = (ImageView) findViewById(R.id.imgspace);
	
	    Bitmap image_bitmap = decodeSampledBitmapFromResource(getResources(), picItem.getId(), 500, 500);
	    m_vwImage.setImageBitmap(image_bitmap);
	    
	    m_vwImage.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(),Image.class);
				intent.putExtra("id", picItem.getId());
				startActivity(intent);
			}
		});
	
	    
	    ListView tagList = (ListView) findViewById(R.id.tagList);
	    ArrayList<String> tagsName = new ArrayList<String>();
	    for (Tag t : picItem.getTags()) {
	    	Log.w("Meu Output",t.getTagName());
	    	tagsName.add(t.getTagName());
	    }
	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	    		  android.R.layout.simple_list_item_1, android.R.id.text1, tagsName);
	    tagList.setAdapter(adapter);
	    //text.setText(picPosition);  

	}
	
	public int calculateInSampleSize(
			BitmapFactory.Options options, int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float)height / (float)reqHeight);
			} else {
				inSampleSize = Math.round((float)width / (float)reqWidth);
			}
		}
		return inSampleSize;
	}
	
	public Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
			int reqWidth, int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeResource(res, resId, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeResource(res, resId, options);
	}
}
